<template>
	<view>
		<view class="tabs">
			<view  @tap="tab(true)" :class="{act:isShow == true}">
				出售中  
			</view>
			<view @tap="tab(false)" :class="{act:isShow == false}">
				已下架  
			</view>
		</view>
		<view v-if="data != '' " class="p30"> 
			<view class="lists ">
				<view v-for="(v,i) in data" :key="i" class="main ">
					<view class="box">
						<view @tap="GoGoodsDetails(v)" v-if="v.isMarketable == false" class="goodsModels">
							<view class="goodsModels_box">
								已下架
							</view>
						</view>
						<image @tap="GoGoodsDetails(v)" class="shop" :src="v.thumbnail+'?x-oss-process=image/resize,m_lfit,h_200,w_200'" mode="aspectFit"></image>
						<view class="txt">
							<p class="p1  one">
								{{v.name}}
							</p>
							<p class="p2   one ">
								收藏 {{v.favorite}}
								&nbsp; &nbsp;
								已售 {{v.sales}}
							</p>
							<p class="p3   one"><text style="font-size: 24rpx;">¥</text> {{v.price}} </p>
						</view>
					</view>
					<view class="bottom ">
						<p class="btnsPublish">
							<u-button @tap="update(v)" :ripple="true" shape="circle">编辑</u-button>
							<u-button v-if="v.isMarketable == true" @tap="productyShelfs(v,i)" :ripple="true" shape="circle">下架</u-button>
							<u-button v-if="v.isMarketable == false" @tap="productyShelvess(v,i)" :ripple="true" shape="circle">上架</u-button>
							<u-button @tap="delect(v,i)" :ripple="true" shape="circle">删除</u-button>
						</p>
					</view>
				</view>
			</view>
		</view>
		<view v-if="data == '' " class=" dataNone">
			<image src="../../static/image/shangpin@2x.png" mode=""></image>
			<p>暂未商品 ~ </p>
			<u-button @tap="GoCreateGoods()" :ripple="true" shape="circle" style="margin: auto; margin-top: 70rpx;" type="success">
				去创建
			</u-button>
		</view>
		<view class="bottomNav">
			<button @tap="go('../CreateDynamic/CreateDynamic')">发布动态</button>
			<u-button @tap="go('../CreateGoods/CreateGoods')" type="success">发布商品</u-button>
		</view>
		<models title='提示' msg="确定要删除" @success="success" ref="model"></models>
	</view>
</template>

<script>
	import {
		myProducts,
		productyShelf,
		productyShelves,
		productyDelete,
		productProductList
	} from '../../api/api.js'
	import {storePersonalStore} from '@/api/api.js'
	export default {
		data() {
			return { 
				isShow:true,
				data: [],
				pageNumber: 1,
				pageSize: 10,
				stops: 0,
				act: []
			};
		},
		onShow() {
			this.data = []
			this.stops = 0
			this.pageNumber = 1
			this.init() 
		},
		onReachBottom() {
			if (this.stops == 0) {
				this.init()
			}
		},
		methods: { 
			tab(type){
				this.isShow = type
				this.data = []
				this.stops = 0
				this.pageNumber = 1
				this.init() 
			},
			go(url){
				this.$navigateTo(url)
			},
			GoCreateGoods() {
				this.$navigateTo('../CreateGoods/CreateGoods') 
			},
			GoGoodsDetails(v) {
				this.$navigateTo('../GoodsDetails/GoodsDetails?id=' + v.id)
			},
			async success() {
				let data = await productyDelete({
					id: this.act.id
				})
				this.data = []
				this.stops = 0
				this.pageNumber = 1
				this.init()
			},
			delect(v) {
				this.act = v
				this.$refs['model'].show()
			},
			// 上架架
			async productyShelvess(v, i) {
				let parameter = {
					id: v.id
				}
				let data = await productyShelves(parameter)
				if (data.code == 0) {
					this.data[i].isMarketable = true
				}
				this.data.splice(i,1)
				if(this.data.length <3){
					this.data = []
					this.stops = 0
					this.pageNumber = 1
					this.init()
				} 
			},
			// 下架
			async productyShelfs(v, i) {
				let parameter = {
					id: v.id
				}
				let data = await productyShelf(parameter)
				if (data.code == 0) {
					this.data[i].isMarketable = false
				}
				this.data.splice(i,1)
				if(this.data.length <3){
					this.data = []
					this.stops = 0
					this.pageNumber = 1
					this.init()
				}  
			},
			update(v) {
				this.$navigateTo('../CreateGoods/CreateGoods?id=' + v.id)
			},
			async init() {
				let parameter = {
					isMarketable:this.isShow,
					pageable: {
						pageNumber: this.pageNumber,
						pageSize: this.pageSize
					}
				}
				let data = await productProductList(parameter)
				data = data.data.productList.content
				if (data.length != this.pageSize) {
					this.stops = 1
				}
				this.data = [...this.data, ...data]
				this.pageNumber = this.pageNumber + 1
			}
		}
	}
</script>

<style lang="less" scoped>
	@import 'index.less';
</style>
